package com.ctg.payroll2.dao.xlsImport.imp;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ctg.platform.orm.ibatis.IbatisGenericDao;
import com.ctg.platform.util.Page;

/**
 * @author Sami
 * 
 */
@SuppressWarnings("unchecked")
public class IbatisXlsImportDAO extends IbatisGenericDao implements
		com.ctg.payroll2.dao.xlsImport.XlsImportDAO {
	public static final String POSTFIX_INSERT_TEMP = "insert_temp";
	public static final String POSTFIX_DELETE_TEMP = "deleteTempData";
	public static final String POSTFIX_INSERT_TEMP2FORMAl = "insert_Tmp2Formal";

	/*
	 * 
	 * 
	 * @see com.ctg.payroll.dao.XlsImportDAO#importData(java.lang.Object,
	 *      java.lang.Object[]) Object... object
	 *      方便以后扩展参数,如果更换数据库环境,可以用List或者其他类型的参数进行操作
	 */
	public void importData(Object o, Object... object) {
		getSqlMapClientTemplate().insert(
				getStatementId(o.getClass(),
						IbatisXlsImportDAO.POSTFIX_INSERT_TEMP), o);
	}

	public List<Object> getMapping(Object o) {
		return getSqlMapClientTemplate().queryForList("ImpXls.query", o);
	}

	/*
	 * @see com.ctg.payroll.dao.XlsImportDAO#deleteTempData(java.lang.Object)
	 *      删除临时导入数据
	 */
	public void deleteTempData(Object o) {
		getSqlMapClientTemplate().delete(
				(getStatementId(o.getClass(),
						IbatisXlsImportDAO.POSTFIX_DELETE_TEMP)));
	}

	/*
	 * @see com.ctg.payroll.dao.XlsImportDAO#pagedQuery_temp(java.lang.Class,
	 *      java.util.Map, int, int, java.lang.Object[]) 查询导入失败纪录,Object...
	 *      o参数已方便以后扩充sqlId的变更
	 */
	public Page pagedQuery_temp(Class entityClass, Map parameterObject,
			int start, int limit, Object... o) {
		return super.pagedQuery(entityClass, parameterObject, start, limit,
				"temp_count", "query_temp");
	}

	/**
	 * 获取一个数据库连接
	 * 
	 * @return
	 * @throws Exception
	 */
	public java.sql.Connection getCon() throws Exception {
		return getSqlMapClient().getDataSource().getConnection();
	}

	/**
	 * 执行本地sql 如果不传入参数,则返回一个打开的数据库连接
	 * 
	 * @param obj
	 * @return
	 */
	public Object executeSql(Object... obj) {
		Object o = null;
		if (obj.length < 0) {
			try {
				o = getSqlMapClient().getDataSource().getConnection();
			} catch (SQLException e) {
				logger.debug("获取连接失败");
				e.printStackTrace();
			}
		} else {
			java.sql.Connection con = null;
			try {
				con = getSqlMapClient().getDataSource().getConnection();
				for (Object object : obj) {
					if (object instanceof List) {
						List<String> li = (List) object;
						int count = li.size();
						int i = 1;
						for (String string : li) {
							PreparedStatement ps = con.prepareStatement(string);
							ps.execute();
							ps.close();
							logger.info("正在执行第" + i + "条插入语句,共" + count + "条 :"
									+ string);
							i++;
						}
					} else {
						con.prepareStatement(object.toString()).execute();
						logger.info(object.toString());
					}
				}
				o = true;
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			} finally {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return o;
	}

	public List<HashMap<String, Object>> getDataforMap(Object parameterObject) {
		return getSqlMapClientTemplate().queryForList(
				"ReadForMap.selectForMap", parameterObject);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ctg.payroll.dao.XlsImportDAO#update_imp_table()
	 *      修改imp_table(薪资导入临时表)的empoyee_id
	 */
	public void update_imp_table(HashMap<String, Object> paramap) {
		getSqlMapClientTemplate().update("ReadForMap.update", paramap);

	}

	private String getStatementId(Class entityClass, String suffix) {
		String className = entityClass.getName();
		String shortName = className.replace(entityClass.getPackage().getName()
				+ ".", "");
		return shortName + "." + suffix;
	}

	/*
	 * @see com.ctg.payroll.dao.XlsImportDAO#insertTmp2Formal(java.lang.Object)
	 *      数据从临时表到正式表
	 */

	public void insertJTmp2Formal(Class o) {
		getSqlMapClientTemplate()
				.insert(
						getStatementId(o,
								IbatisXlsImportDAO.POSTFIX_INSERT_TEMP2FORMAl));

	}

	/*
	 * @see com.ctg.payroll.dao.XlsImportDAO#insertTmp2Formal(java.lang.Object)
	 *      数据从临时表到正式表/带参数的
	 */
	public void insertJTmp2Formal(Class o, HashMap<String, Object> paraMap) {

		getSqlMapClientTemplate()
				.insert(
						getStatementId(o,
								IbatisXlsImportDAO.POSTFIX_INSERT_TEMP2FORMAl),
						paraMap);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ctg.payroll2.dao.xlsImport.XlsImportDAO#payClassIsOnly(java.lang.Object)
	 *      实现导入薪资项是否唯一,同一个公司,一个年月,一个类型只允许一条记录
	 */
	public Boolean payClassIsOnly(Object paraMap) {
		return (Integer) getSqlMapClientTemplate().queryForObject(
				"ReadForMap.payClassIsOnly", (Map) paraMap) >= 1 ? false : true;
	}

	/**
	 * 导入社保项目是否唯一,同一个公司,一个年月,一个类型只允许一条记录
	 * 
	 * @param paraMap
	 * @return
	 */
	public Boolean socialIsOnly(Object paraMap) {
		return (Integer) getSqlMapClientTemplate().queryForObject(
				"ReadForMap.socialIsOnly", (Map) paraMap) >= 1 ? false : true;
	}

	/*
	 * (non-Javadoc) 检查导入雇员是否唯一
	 * 
	 * @see com.ctg.payroll2.dao.xlsImport.XlsImportDAO#isOnlyByempCsCode(java.lang.Object[])
	 */
	public List<Map<String, Object>> isOnlyByempCsCode(Object... obj) {
		return this.getSqlMapClientTemplate().queryForList(
				"PbEmployee.isOnlyByempCsCode");
	}

	/*
	 * (non-Javadoc) 雇员导入从临时表到正式表之前修改 政治面貌,部门,婚姻状况等下拉框形式的值为对应数据库的值
	 * 
	 * @see com.ctg.payroll2.dao.xlsImport.XlsImportDAO#updateEmpcomboxMapping()
	 */
	public Integer updateEmpcomboxMapping() {
		return this.getSqlMapClientTemplate().update(
				"PbEmployee.updateEmpcomboxMapping");
	}

}
